package com.lzxuni.modules.dataAcquisition.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.common.entity.Tree;
import com.lzxuni.modules.dataAcquisition.entity.Chain;
import com.lzxuni.modules.dataAcquisition.mapper.ChainMapper;
import com.lzxuni.modules.dataAcquisition.service.ChainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 追溯链条 服务实现类
 * </p>
 *
 * @author wzx
 * @since 2019-06-25
 */
@Service
public class ChainServiceImpl extends ServiceImpl<ChainMapper, Chain> implements ChainService {

    @Autowired
    private ChainMapper chainMapper;

    @Override
    public PageInfo<Chain> queryPage(PageParameter pageParameter, Chain chain) throws SQLException {
        PageHelper.startPage(pageParameter.getPage(), pageParameter.getRows()).setOrderBy(
                pageParameter.getSidx() + " " + pageParameter.getSord());
        List<Chain> list;
        if (StringUtils.isNotEmpty(chain.getChainName())) {
            list = baseMapper.selectList(new QueryWrapper<Chain>().like("chain_name", chain.getChainName()));
        }else {
            list = baseMapper.selectList(new QueryWrapper<>());
        }
        PageInfo<Chain> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<Tree> getTreeSelectTree(String nature) {
        List<Tree> list = new ArrayList<>();
        List<Chain> chains = baseMapper.selectList(new QueryWrapper<>());
        if (chains != null && chains.size() > 0) {
            for (Chain chain : chains) {
                Tree tree = new Tree();
                tree.setId(chain.getChainId());
                tree.setParentId("");
                tree.setText(chain.getChainName());
                tree.setTitle(chain.getChainName());
                tree.setShowcheck(false);
                tree.setCheckstate(0);
                tree.setComplete(true);
                list.add(tree);
            }
        }
        return list;
    }
    @Override
    public List<Tree> getTreeSelectTree2(String nature) {
        List<Tree> list = new ArrayList<>();
        List<Chain> chains = baseMapper.selectList(new QueryWrapper<>());
        if (chains != null && chains.size() > 0) {
            for (Chain chain : chains) {
                Tree tree = new Tree();
                tree.setId(chain.getChainName());
                tree.setParentId("");
                tree.setText(chain.getChainName());
                tree.setTitle(chain.getChainName());
                tree.setShowcheck(false);
                tree.setCheckstate(0);
                tree.setComplete(true);
                list.add(tree);
            }
        }
        return list;
    }
}
